package com.example.springboot01.service;


import com.example.springboot01.entity.AddressInfo;
import com.example.springboot01.entity.UserInfo;
import com.example.springboot01.mapper.AddressInfoMapper;
import com.example.springboot01.mapper.UserInfoMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service //告诉spring框架，我这个类是可以自动实例化的。就是自动new一个对象的意思。
public class AddressInfoService {
    @Autowired
    private AddressInfoMapper addressInfoMapper;
//
//    public boolean checkLogin(String userCode, String userPwd){
//        boolean flag = false;
//        UserInfo userInfo = null;
//        userInfo = userInfoMapper.checkLogin(userCode, userPwd);
//        //if (!userInfo.equals(null))
//        if (userInfo != null)
//            flag = true;
//        return flag;
//    }
//
//    public UserInfo checkLoginR(String userCode, String userPwd){
//        return  userInfoMapper.checkLogin(userCode, userPwd);
//    }
//
//    public List<UserInfo> qryUserList(String field_name, String field_value){
//        List<UserInfo> user_list = new ArrayList<UserInfo>();
//        user_list = userInfoMapper.qryUserList(field_name, field_value);
//
//        return user_list;
//    }
    public int insertAAddress(AddressInfo addressInfo,String user_code){
        return addressInfoMapper.insertAddress(addressInfo,user_code);
    }
//
//    public UserInfo getAUserInfo(String user_code){
//        return userInfoMapper.getAUserInfo(user_code);
//    }
    public int   updateDefaultAddress(int maxId){
        return addressInfoMapper.updateDefaultAddress(maxId);
    }

    //将所有DefultAddress值设置为0
    public int resetAllDefaultAddress(String userId){
        return addressInfoMapper.resetAllDefaultAddress(userId);
    }

    public int   getMaxAddressId(){
        return addressInfoMapper.getMaxAddressId();
    }
    public AddressInfo getAddressInfo(String address_id){
        return addressInfoMapper.getAddressInfo(address_id);

    }
    public int editAddress(AddressInfo addressInfo){
        return addressInfoMapper.editAddress(addressInfo);
    }
    public int deleteAddressInfo(String address_id){
        AddressInfo deletedAddress = addressInfoMapper.getAddressById(address_id);

        // 将记录标记为已删除
        addressInfoMapper.deleteAddress(address_id);

        int deleted_Default_address= Integer.parseInt(deletedAddress.getDefault_address());
        System.out.println("Default_address:"+deleted_Default_address);
        return deleted_Default_address;
    }
    public List<AddressInfo> qryAddressPageList(int start_page, int page_size,String user_code){
        //先把当前页转换成数据库里面的当前行
        int current_pos = (start_page - 1) * page_size;
        return addressInfoMapper.qryUserPageList(current_pos, page_size,user_code);
    }
}
